<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="breakthroughQuery.name" placeholder="副本名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="primary" @click="clearFilter">
        查看所有
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加副本
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="breakthroughs"
      fit
      highlight-current-row
      style="width: 98%;"
      @sort-change="sortChange"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-card class="box-card">
              <el-form-item label="描述">
                <span v-html="props.row.description" />
              </el-form-item>
            </el-card>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="120">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="副本名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关卡数量" width="320" align="center">
        <template slot-scope="{row}">
          <span>{{ row.nodes.length }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通关奖励(ACB)" width="200" align="center">
        <template slot-scope="{row}">
          <span>{{ row.rewardAcb }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="170" class-name="small-padding">
        <template slot-scope="{row,$index}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作菜单<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleUpdate(row, $index)">修改</el-dropdown-item>
              <el-dropdown-item @click.native="currentRow = row, currentIndex = $index, deleteDialogVisible = true">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="breakthroughQuery.page" :limit.sync="breakthroughQuery.limit" @pagination="getBreakthroughs" />

    <el-dialog
      title="提示"
      :visible.sync="deleteDialogVisible"
      width="30%"
    >
      <span>确定删除该副本？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">否</el-button>
        <el-button type="primary" @click="deleteBreakthrough">是</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import { fetchBreakthroughList, deleteBreakthrough } from '@/api/challenge/breakthrough'
import waves from '@/directive/waves' // waves指令
import Pagination from '@/components/Pagination' // 基于el-pagination

export default {
  name: 'List',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      currentRow: '',
      currentIndex: '',
      breakthroughs: null,
      challengeProblems: null,
      total: 0,
      listLoading: true,
      breakthroughQuery: {
        page: 1,
        limit: 10,
        sort: undefined,
        name: undefined
      },
      deleteDialogVisible: false
    }
  },
  created() {
    this.getBreakthroughs()
  },
  methods: {
    getBreakthroughs() {
      this.listLoading = true
      fetchBreakthroughList(this.breakthroughQuery).then(response => {
        const res = response.data
        this.breakthroughs = res.datas[0]
        this.total = res.datas[1]
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },
    handleFilter() {
      this.breakthroughQuery.page = 1
      this.getBreakthroughs()
    },
    clearFilter() {
      this.breakthroughQuery = {
        page: 1,
        limit: 10,
        sort: undefined,
        name: undefined
      }
      this.getBreakthroughs()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.breakthroughQuery.sort = order
        this.handleFilter()
      }
    },
    handleCreate() {
      this.$router.push({
        path: '/challenge/breakthrough/create'
      })
    },
    handleUpdate(row) {
      this.$router.push({
        path: '/challenge/breakthrough/update',
        query: { row }
      })
    },
    deleteBreakthrough() {
      this.deleteDialogVisible = false
      const row = this.currentRow
      const index = this.currentIndex
      deleteBreakthrough(row.id).then(response => {
        const res = response.data
        if (res.code === 10000) {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.breakthroughs.splice(index, 1)
        }
      })
    }
  }
}
</script>
